package com.qf.service.impl;

import com.qf.dao.TbThirdPictureRepository;
import com.qf.pojo.entity.TbFilmPicture;
import com.qf.pojo.entity.TbThirdPicture;
import com.qf.pojo.request.TbFilmPictureReq;
import com.qf.pojo.request.TbThirdPictureReq;
import com.qf.pojo.response.BaseResponse;
import com.qf.service.TbFilmPictureService;
import com.qf.service.TbThirdPictureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbThirdPictureServiceImpl implements TbThirdPictureService {
    @Autowired
    TbThirdPictureRepository tbThirdPictureRepository;
    /**
     * 查询所有电影图片
     *
     * @param page 当前页
     * @param size 当前页显示行数
     * @return 响应对象
     */
    public BaseResponse findAll(Integer page, Integer size) {
        PageRequest of = PageRequest.of(page - 1, size);
        Page<TbThirdPicture> all = tbThirdPictureRepository.findAll(of);
        List<TbThirdPicture> tbFilmPictures = all.getContent();
        long total = all.getTotalElements();
        return new BaseResponse(1,"success",tbFilmPictures,total);
    }

    /**
     * 新增或者修改理财图片
     *
     * @param tbThirdPictureReq 理财图片请求对象
     * @return 响应对象
     */
    public BaseResponse updateOrSave(TbThirdPictureReq tbThirdPictureReq) {
        TbThirdPicture tbThirdPicture = new TbThirdPicture();
        BeanUtils.copyProperties(tbThirdPictureReq,tbThirdPicture);
        tbThirdPictureRepository.saveAndFlush(tbThirdPicture);
        return new BaseResponse().SUCCESS();
    }

    /**
     * 删除理财图片
     *
     * @param id 理财图片id
     * @return 响应对象
     */
    public BaseResponse deleteById(Integer id) {
        tbThirdPictureRepository.deleteById(id);
        return new BaseResponse().SUCCESS();
    }
}
